package com.lhc.service.impl;

import com.lhc.dao.UserDao;
import com.lhc.dao.impl.UserDaoImpl;
import com.lhc.pojo.ResultVO;
import com.lhc.pojo.User;
import com.lhc.service.UserService;

import java.sql.SQLException;

/**
 * @author lhc
 * @version V1.0
 * @date 2021/06/30
 **/
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    /**
     * 添加用户
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(User user) throws SQLException {
        return userDao.insert(user);
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    @Override
    public ResultVO login(String username, String password) throws SQLException {
        User user = userDao.findByName(username);
        ResultVO resultVO = new ResultVO();
        // 判断User对象
        if (user != null){
            /**
             * 用户登录验证
             *  密码加密---MD5
             */
            if (user.getPassword().equals(password)){
                resultVO.setSuccess(true);
                resultVO.setMessage("登录成功");
                resultVO.setData(user);
                return resultVO;
            }
            else{
                // 用户不存在
                resultVO.setSuccess(false);
                resultVO.setMessage("用户不存在");
                return resultVO;
            }
        }
        return null;
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     * @throws SQLException
     */
    @Override
    public User findByName(String username) throws SQLException {
        return userDao.findByName(username);
    }
}
